package com.example.testapp.activity.activity.meact;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.model.ImageItem;
import com.example.testapp.activity.view.HackyViewPager;
import com.example.testapp.activity.view.bitmap.BitmapCache;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by l on 2016/7/11.
 */
public class ImagePagerActivity extends AppCompatActivity {

    public static final String EXTRA_IMAGE_LIST = "imageList";

    private HackyViewPager mPager;
    private TextView  indictor = null;
    public List<ImageItem> imageList;
    public ImagePagerAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        imageList = (List<ImageItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);

        initView();
    }

    private void initView() {
        indictor = (TextView)findViewById(R.id.indicator);

        mPager = (HackyViewPager)findViewById(R.id.viewpager);
        mAdapter = new ImagePagerAdapter(this,  imageList);
        mPager.setAdapter(mAdapter);

        CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
        indictor.setText(text);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                CharSequence text = getString(R.string.viewpager_indicator, position+1, mPager.getAdapter().getCount());
                indictor.setText(text);
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    public class ImagePagerAdapter extends PagerAdapter {
        final String TAG = getClass().getSimpleName();
        public List<ImageItem>  itemList;
        public BitmapCache cache;
        public Context mContext;

        BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
            @Override
            public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
                if(imageView != null && bitmap != null){
                    String urls = (String) params[0];
                    if(urls != null && urls.equals(imageView.getTag())){
                        imageView.setImageBitmap(bitmap);
                    }else{
                        Log.e(TAG, "callback, bmp not match");
                    }
                }else{
                    Log.e(TAG, "callback, bmp null");
                }
            }
        };

        public ImagePagerAdapter(Context context, List<ImageItem> urls) {
            this.mContext = context;
            this.itemList =  urls;
            cache = new BitmapCache();
        }

        @Override
        public int getCount() {
            return itemList == null ? 0 :itemList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            /*PhotoView photoView = new PhotoView(container.getContext());
            ImageItem item = mListFile.get(position);
            cache.displayBitmap(photoView, item.thumbnailPath, item.imagePath, callback);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;*/
            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
              container.removeView((View) object);
        }
    }
}
